package com.todo.user.application.api.controller;

import com.todo.common.common.entity.response.BizResponse;
import com.todo.user.api.UserAccountApi;
import com.todo.user.api.request.QueryUserAccountByUserNameRequest;
import com.todo.user.api.request.RegisterUserAccountByNameRequest;
import com.todo.user.api.request.RemoveUserAccountByUserNameRequest;
import com.todo.user.api.request.UpdateUserSecretRequest;
import com.todo.user.api.response.UserAccountResponse;
import com.todo.user.application.convert.UserInfoConvert;
import com.todo.user.application.entity.bo.UserAccountBO;
import com.todo.user.application.service.UserAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2025/2/21 02:09
 * @description
 */
@RestController
@RequestMapping("/api/userAccount")
@Tag(name = "服务端-用户账号接口", description = "服务端-用户账号接口")
public class UserAccountApiController implements UserAccountApi {

    @Resource
    private UserAccountService userAccountService;


    @Override
    @Operation(summary = "根据用户名注册")
    @PostMapping("/registerUserAccountByName")
    public BizResponse<Boolean> registerUserAccountByName(@RequestBody RegisterUserAccountByNameRequest request) {
        if (request == null || request.getUserName() == null || request.getPassword() == null) {
            return BizResponse.paramError();
        }
        Boolean register = userAccountService.insertUserAccount(request.getUserName(), null, request.getPassword());
        return BizResponse.success(register);
    }

    @Override
    @Operation(summary = "根据用户名查询用户信息")
    @PostMapping("/queryUserAccountByUserName")
    public BizResponse<UserAccountResponse> queryUserAccountByUserName(@RequestBody QueryUserAccountByUserNameRequest request) {
        if (request == null || request.getUserName() == null) {
            return BizResponse.paramError();
        }
        UserAccountBO userAccountBO = userAccountService.queryUserAccount(request.getUserName());
        return BizResponse.success(UserInfoConvert.convertToUserAccountResponse(userAccountBO));
    }

    @Override
    @Operation(summary = "更新用户密码")
    @PostMapping("/updateUserSecret")
    public BizResponse<Boolean> updateUserSecret(@RequestBody UpdateUserSecretRequest request) {
        if (request == null) {
            return BizResponse.paramError();
        }
        Boolean updated = userAccountService.updateUserSecret(request.getUserName(), request.getOldPassword(), request.getNewPassword());
        return BizResponse.success(updated);
    }

    @Override
    @Operation(summary = "根据用户名删除用户信息(注销)")
    @PostMapping("/removeUserAccountByUserName")
    public BizResponse<Boolean> removeUserAccountByUserName(@RequestBody RemoveUserAccountByUserNameRequest request) {
        if (request == null || request.getUserName() == null) {
            return BizResponse.paramError();
        }
        Boolean removed = userAccountService.removeUserAccount(request.getUserName());
        return BizResponse.success(removed);
    }

    @Override
    @Operation(summary = "根据用户id查询用户信息")
    @PostMapping("/queryUserAccountByUserId")
    public BizResponse<UserAccountResponse> queryUserAccountByUserId(@RequestBody Long userId) {
        if (userId == null || userId < 0) {
            return BizResponse.paramError();
        }
        UserAccountBO userAccountBO = userAccountService.queryUserAccountById(userId);
        return BizResponse.success(UserInfoConvert.convertToUserAccountResponse(userAccountBO));
    }

}
